function img = ImgProc(img, hROI, ImgMean, scale, bTransform)

% img = Img.getROI(img, InputDim(1:3));
img = hROI(img);

% if InputDim(3) == 1
%     img = rgb2gray(img);
% end
img = single(img);

if ~isempty(ImgMean)
    if ~isvector(ImgMean)
        img = bsxfun(@minus, img, ImgMean);
    else
        for i = 1 : size(img, 3)
            img(:,:,i) = img(:,:,i) - ImgMean(i);
        end
    end
end

img = img / scale;

if ~bTransform
    return
end

% permute width and height
switch size(img, 3)
    case 1
        img = img';
    case 3
        % permute channels from RGB to BGR for colored images
        img = img(:, :, [3, 2, 1]);
        img = permute(img, [2, 1, 3]);
end